﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace BezierSegmentDemo
{
    public class ArcFigure : Control
    {
        #region StartPoint
        /// <summary>
        /// StartPoint Dependency Property
        /// </summary>
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
                "StartPoint",
                typeof(Point),
                typeof(ArcFigure),
                new FrameworkPropertyMetadata(
                    new Point(0, 0),
                    new PropertyChangedCallback(StartPointPropertyChanged)));

        private static void StartPointPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ArcFigure figure = d as ArcFigure;
            if (figure != null)
            {
                Point oldP = (Point)e.OldValue;
                Point newP = (Point)e.NewValue;
                figure.EndPoint = new Point(figure.EndPoint.X, newP.Y);
                figure.CenterBezierPoint = new Point(figure.CenterBezierPoint.X + (newP.X - oldP.X) / 2, figure.CenterBezierPoint.Y + (newP.Y - oldP.Y) / 2);
            }
        }

        /// <summary>
        /// Gets or sets the StartPoint property
        /// </summary>
        public Point StartPoint
        {
            get { return (Point)GetValue(StartPointProperty); }
            set { SetValue(StartPointProperty, value); }
        }
        #endregion

        #region EndPoint
        /// <summary>
        /// EndPoint Dependency Property
        /// </summary>
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
                "EndPoint",
                typeof(Point),
                typeof(ArcFigure),
                new FrameworkPropertyMetadata(
                    new Point(300, 0),
                    new PropertyChangedCallback(EndPointPropertyChanged)));

        private static void EndPointPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ArcFigure figure = d as ArcFigure;
            if (figure != null)
            {
                Point oldP = (Point)e.OldValue;
                Point newP = (Point)e.NewValue;
                figure.StartPoint = new Point(figure.StartPoint.X, newP.Y);
                figure.CenterBezierPoint = new Point(figure.CenterBezierPoint.X + (newP.X - oldP.X) / 2, figure.CenterBezierPoint.Y + (newP.Y - oldP.Y) / 2);
            }
        }

        /// <summary>
        /// Gets or sets the EndPoint property
        /// </summary>
        public Point EndPoint
        {
            get { return (Point)GetValue(EndPointProperty); }
            set { SetValue(EndPointProperty, value); }
        }
        #endregion

        #region CenterBezierPoint
        public Point CenterBezierPoint
        {
            get { return (Point)GetValue(CenterBezierPointProperty); }
            set { SetValue(CenterBezierPointProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CenterBezierPoint.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CenterBezierPointProperty =
            DependencyProperty.Register("CenterBezierPoint", typeof(Point), typeof(ArcFigure), new PropertyMetadata(new Point(150, 100), new PropertyChangedCallback(CenterBezierPointPropertyChanged)));

        private static void CenterBezierPointPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ArcFigure figure = d as ArcFigure;
            if (figure != null)
            {
                Point p = (Point)e.NewValue;
                double x = figure.StartPoint.X + (figure.EndPoint.X - figure.StartPoint.X) / 2;
                double y = p.Y;
                if (p.Y > figure.StartPoint.Y)
                {
                    y = p.Y + p.Y - figure.StartPoint.Y;
                }
                else
                {
                    y = p.Y - (figure.StartPoint.Y - p.Y);
                }

                figure.StartBezierPoint = new Point(x, y);
            }
        }
        #endregion

        #region StartBezierPoint
        /// <summary>
        /// StartBezierPoint Dependency Property
        /// </summary>
        public static readonly DependencyProperty StartBezierPointProperty = DependencyProperty.Register(
                "StartBezierPoint",
                typeof(Point),
                typeof(ArcFigure),
                new FrameworkPropertyMetadata(new Point(150, 200)));

        /// <summary>
        /// Gets or sets the StartBezierPoint property
        /// </summary>
        public Point StartBezierPoint
        {
            get { return (Point)GetValue(StartBezierPointProperty); }
            private set { SetValue(StartBezierPointProperty, value); }
        }
        #endregion

        static ArcFigure()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ArcFigure), new FrameworkPropertyMetadata(typeof(ArcFigure)));
        }
    }
}
